const BASE_URL = "https://api.baiweihu.com/";
export const request = (url, { method = 'get', data = {}, success = '', fail = '' } = url) => {
	return new Promise( (resolve, reject) => {
		if( typeof url == 'object') {
			if(typeof url.url != 'string') {
				throw new Error('请求Url格式不正确');
			} else {
				url = url.url;
			}
		}
		let token = '';
		token = uni.getStorageSync('access_token');
		const header_tmp = {
			'Authorization': "Bearer " + token
		};
		uni.request({
			url: BASE_URL + url,
			method,
			data,
			header: header_tmp,
			success: res => {
				if( res.data.code === 401 ) {
					uni.clearStorage();
					// 登录过期，重新登录
					uni.showToast({
						icon: 'none',
						title: '登录过期，请重新登录'
					})
					uni.reLaunch({url: '/pages/index/login'});
				}
				if( res.data.code !== 401 && res.data.code !== 200 ) {
					return uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
				}
				resolve(res.data);
			},
			fail: err => {
				return uni.showToast({
					icon: 'none',
					title: "请求接口失败"
				})
				reject(err)
			}
		})
	})
}
